package org.eclipse.epsilon.emc.rdbms.core.sqlstatement;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public  class SelectWithJoins extends SQLStatement{
	
	/** This method is to generate the SQL Statement to select an element from the database
	*@param tableInfo - The resultset containing the database,schema and table information
	*@param columnNames - This is the list of columns\attributes to be retrieved
	*@param PKTables - The primary table for the join.
	*@Param FKTables - The foreign table for the join
	*@Param PKCols - The columns to be retrieved from the primary table
	*@param FKCols - The columns to be retrieved from the foreign table
	*@param parameters - This is the hashmap of attributes and values that will be specified in the where clause.
	*@return statement - This is the generated SQL Statement to delete the object from the database
	*/	
    public String BuildForCriteria(ResultSet tableInfo, ArrayList<String> columnNames, ArrayList PKtables, ArrayList FKtables, ArrayList PKcols, ArrayList FKcols, HashMap<String, Object> parameters) throws SQLException
    {
    	while (tableInfo.next())
		{
			databaseName = tableInfo.getString("TABLE_CAT");
			schemaName = tableInfo.getString("TABLE_SCHEM");
			tableName = tableInfo.getString("TABLE_NAME");	
		}	
    	
   	 ArrayList removeColumns = new ArrayList();
   	 String statement = "SELECT "+ getStringFromArrayList(columnNames, removeColumns) +" FROM ";
   	 //Now we need to add in a where clause if appropriate
   	 if(PKtables !=null)
   	 {
   		if(PKtables.size() > 0 )
   	    {
   		 statement = statement +FKtables.get(0) +" pktable" + getJoins(databaseName, schemaName, tableName,PKtables,FKtables,PKcols,FKcols,"INNER JOIN");
   	    }
   	 } 
   	 else 
   	 {
   		 statement = statement +databaseName+"."+schemaName+"." + tableName.toString();
   	 }    	 
   	 if(parameters !=null)
   	 {
   	   if(parameters.size()>0)
   	   {
   	   //There is a where clause attached to the query and therefore we need to add it onto the string.
   	   statement = statement + " WHERE "+"pktable"+"."+getStringFromHashMap(parameters,"and");	 
   	   } 
   	 }
    	     return statement;
    }
    
    
    /** This method generates the join clause on the tables.
	*@param databaseName - This is the database Name that the tables belong to
	*@param schemaName  -  This is the schema name that the tables belong to
	*@param tableName   - This is the tablename from to be joined on.
	*@param FKtables    - This is the Foreign tables to be joined
	*@param PKtables     - This is the primary tables to be joined
	*@param PKcols     -  These are the cols to be joined on
	*@Param FKCols    - This is the Foreign Key table column to be joined on
	*@param joinType  - This is the type of join e.g inner left outer etc.
	*/	
    private String getJoins(String databaseName,String schemaName,String tableName, ArrayList PKtables, ArrayList FKtables,ArrayList PKcols, ArrayList FKcols,  String joinType)
	{
    	String statement = "";
	  	 for (int i=0;i<PKtables.size();i++)
		 { 
      		statement = statement + " "+ joinType+ " ";
      		statement = statement + PKtables.get(i) + " fktable ON ";
      		statement = statement + FKcols.get(i)+" = "+PKcols.get(i) +""; 
        }               
		return statement;
	}
    
    /** This method is not implemented
	*/	
    @Override
	public String BuildForCriteria(ResultSet sqlTables, HashMap properties,
			HashMap<String, Object> props) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

    /** This method is not implemented
	*/	
    @Override
	public String BuildForCriteria(ResultSet sqlTables,
			HashMap<String, Object> props) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

    /** This method is not implemented
	*/	
	@Override
	public String BuildForCriteria(ResultSet tableInfo,
			ArrayList<String> properties, HashMap<String, Object> criteria)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/** This method is not implemented
	*/	
	@Override
	public String BuildForCriteria(ResultSet tableInfo, ResultSet sqlColumns,
			ResultSet primaryKey, ResultSet columnMetaData,
			HashMap<String, Object> properties) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}	
}
